package com.ccgk.logistics.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.ccgk.common.page.PaginationVo;
import com.ccgk.logistics.domain.Company;
import com.ccgk.logistics.domain.Container;
import com.ccgk.logistics.domain.ContainerExample;
import com.ccgk.logistics.persistence.ContainerMapper;
import com.ccgk.logistics.service.ContainerService;

@Service
public class ContainerServiceImpl implements ContainerService{
	
	@Autowired
	private ContainerMapper containerMapper;
	
	@Override
	public Integer add(Container container) {
		return containerMapper.insert(container);
	}

	@Override
	public Integer delete(int id) {
		return containerMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Container> findAll() {
		return containerMapper.selectByExample(null);
	}

	@Override
	public Container getById(int id) {
		return containerMapper.selectByPrimaryKey(id);
	}

	@Override
	public Integer update(Container container) {
		return containerMapper.updateByPrimaryKeySelective(container);
	}
	
	@Override
	public Container getBySize(String size){
		if(StringUtils.isBlank(size))
			return null;
		ContainerExample example = new ContainerExample();
		example.createCriteria().andSizeEqualTo(size);
		List<Container> result = containerMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(result))
			return null;
		return result.get(0);
	}

	@Override
	public PaginationVo<Container> findList(ContainerCriteria criteria,
			int currentPage, int pageSize) {
		ContainerExample example = new ContainerExample();
		ContainerExample.Criteria _criteria = example.createCriteria();
		Map<Integer,Object> params = criteria.getParams();
		for(Integer key : params.keySet()){
			if(key.equals(1)){
				_criteria.andSizeLike(params.get(key).toString());
			}
		}
		example.setOrderByClause("size asc");
		List<Container> data = containerMapper.selectByExample(example,new RowBounds((currentPage-1)*pageSize, pageSize));
		int recordCount = containerMapper.countByExample(example);
		PaginationVo<Container> result = new PaginationVo<Container>(data, recordCount, pageSize, currentPage);
		return result;
	}

	@Override
	public void batchDelete(Integer[] idIntArray) {
		for(Integer id:idIntArray){
			delete(id);
		}
	}

}
